package com.example.chessbattleplatform.web.room_board.Service;

import cn.hutool.json.JSONUtil;
import com.example.chessbattleplatform.Pojo.Exception.BusinessException;
import com.example.chessbattleplatform.Pojo.Exception.StatusCode;
import com.example.chessbattleplatform.utils.ThreadJwtParseUtils;
import com.example.chessbattleplatform.web.room_board.Pojo.Vo.Pieces;
import com.example.chessbattleplatform.web.room_board.Pojo.Vo.PiecesDataVo;
import com.example.chessbattleplatform.web.room_board.Pojo.dto.PiecesDto;
import com.example.chessbattleplatform.web.room_board.Service.impl.RoomBoardServiceInterface;
import com.example.chessbattleplatform.web.room_board.dataService.impl.RoomBoardDataServiceInterface;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.UUID;

@Service
@RequiredArgsConstructor
public class RoomBoardService implements RoomBoardServiceInterface {
    // 导入jwt解析
    private final ThreadJwtParseUtils threadJwtParseUtils;
    // 导入数据服务
    private final RoomBoardDataServiceInterface roomBoardDataService;

    @Override
    public PiecesDataVo initRoomBoard(PiecesDto piecesDto) {
        String BlackUserId, WhiteUserId;

        try {

            BlackUserId = threadJwtParseUtils.getTokenToUserId(piecesDto.getBlackJwtKen());
            WhiteUserId = threadJwtParseUtils.getTokenToUserId(piecesDto.getWhiteJwtKen());
        } catch (Exception e) {
            throw new BusinessException(StatusCode.MISSING_PARAMETER, "缺失用户参数");
        }

        PiecesDataVo piecesDataVo = initPiecesDataVo();
        String jsonStr = JSONUtil.toJsonStr(piecesDataVo);


        if (!roomBoardDataService.addRoom(UUID.randomUUID().toString(), BlackUserId, WhiteUserId, jsonStr)) {
            throw new BusinessException(StatusCode.ERROR_CODE, "创建房间失败");
        }

        return piecesDataVo;
    }

    @Override
    public PiecesDataVo getRoom(Long userId) {
        return initPiecesDataVo();
    }

    private PiecesDataVo initPiecesDataVo() {
        int[] PiecesDataX = {0, 1, 2, 3, 4, 5, 6, 7, 8, 1, 7, 0, 2, 4, 6, 8};
        int[] blackPiecesDataY = {0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 3, 3, 3, 3, 3};
        int[] redPiecesDataY = {9, 9, 9, 9, 9, 9, 9, 9, 9, 7, 7, 6, 6, 6, 6, 6};
        String[] blackPiecesDataType = {"车", "马", "象", "士", "将", "士", "象", "马", "车", "炮", "炮", "卒", "卒", "卒", "卒", "卒"};
        String[] redPiecesDataType = {"车", "马", "相", "仕", "帅", "仕", "相", "马", "车", "炮", "炮", "兵", "兵", "兵", "兵", "兵"};

        ArrayList<Pieces> PiecesData = new ArrayList<>();

        for (int i = 0; i < PiecesDataX.length; i++) {
            PiecesData.add(new Pieces(blackPiecesDataType[i], "black", PiecesDataX[i], blackPiecesDataY[i]));
        }
        for (int i = 0; i < PiecesDataX.length; i++) {
            PiecesData.add(new Pieces(redPiecesDataType[i], "red", PiecesDataX[i], redPiecesDataY[i]));
        }

        return new PiecesDataVo(PiecesData, "red");
    }

}
